%{
// #include "y.tab.h"
#include <string>
#include "tinytest.h"
#define yylval cool_yylval
#define yylex  cool_yylex
using namespace std;

extern FILE *fin; /* we read from this file */
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
	if ( (result = fread( (char*)buf, sizeof(char), max_size, fin)) < 0) \
		YY_FATAL_ERROR( "read() in flex scanner failed");

extern YYSTYPE cool_yylval;
extern int curr_lineno;
extern std::map<std::string, int> string_mp; //符号值
extern std::vector<std::string> string_table;//符号表

%}

%%
\n                { ++curr_lineno; }
"if"              { return IF; }     // 列出所有符号命令
"then"            { return THEN; }
"else"            { return ELSE; }
"end"             { return END; }
"until"           { return UNTIL; }
"read"            { return READ; }
"write"           { return WRITE; }
[a-z][a-zA-Z0-9_]* {

                    cool_yylval.str_id = string_table.size();
                    string_table.push_back(string(yytext));
                    return ID ; } //变量
[0-9]+            { cool_yylval.num = atoi(yytext); return NUMBER; } // 识别数字，并转化为整数
":="              { return ASSIGN; }
"="               { return EQ; }
"<"               { return LT; }
">"               { return GT; }
"+"                  { return PLUS; }
"-"                  { return MINUS; }
"*"                  { return TIMES; }
"/"                  { return OVER; }
"("                  { return LPAREN; }
")"                  { return RPAREN; }
";"                  { return SEMI; }
"{"[^\}]*"}"          {    if(yytext[0]=='\n')
                            ++curr_lineno;} //什么都不作

[\[\]\"\']        {     cool_yylval.error_id = string_table.size();
                        string_table.push_back("wrong symbol "+string(yytext));
                        return ERROR; }
">="                { 
                    
                    return ERROR; 
                    }
" "                 { /* ignore whitespace */ }

%%


